package org.jeecg.modules.inquiry;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.modules.doctor.entity.Doctor;
import org.jeecg.common.modules.doctor.service.IDoctorService;
import org.jeecg.common.modules.inquiry.vo.InquiryReqVo;
import org.jeecg.common.modules.patient.vo.PatientReqVo;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.modules.inquiry.entity.InquiryInfo;
import org.jeecg.common.modules.inquiry.service.IInquiryInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.system.vo.InquiryInfoVo;
import org.jeecg.modules.system.vo.PatientResponseVo;
import org.jeecg.modules.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: inquiry_info
 * @Author: jeecg-boot
 * @Date:   2020-09-02
 * @Version: V1.0
 */
@Api(tags="inquiry_info")
@RestController
@RequestMapping("/inquiry/inquiryInfo")
@Slf4j
public class InquiryInfoController extends JeecgController<InquiryInfo, IInquiryInfoService> {
	@Resource
	private IInquiryInfoService inquiryInfoService;

	@Resource
	private IDoctorService doctorService;
	/**
	 * 分页列表查询
	 *
	 * @param inquiryInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "inquiry_info-分页列表查询")
	@ApiOperation(value="inquiry_info-分页列表查询", notes="inquiry_info-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(InquiryInfo inquiryInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		//数据开始的记录数
		int dataStart = (pageNo - 1) * pageSize;

		//先查询所有的患者、用户信息
		String id     = req.getParameter("id");
		String patientName = req.getParameter("patientName");
		String userName    = req.getParameter("userName");
		String doctorName  = req.getParameter("doctorName");
		String type        = req.getParameter("type");
		List<InquiryReqVo> inquiryInfos = inquiryInfoService.getInquiryInfo(id,patientName,userName,doctorName,type,dataStart,pageSize);

		//查询到的总数据数
		Integer count = inquiryInfoService.getInquiryCount(id,patientName,userName,doctorName,type);

		//组合分页展示的数据
		ResultVo vo = this.initResultVo(inquiryInfos,count,pageNo,pageSize);

		return Result.ok(vo);
	}

	 /**
	  * 组合返回的信息
	  * @param inquiryInfos
	  * @param count
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 private ResultVo initResultVo(List inquiryInfos, Integer count, Integer pageNo, Integer pageSize){
		 ResultVo vo = new ResultVo();
		 double dCount    = Double.valueOf(count);       //查询的数据总条数
		 double dPageSize = Double.valueOf(pageSize);    //每页显示的条数
		 int pages = (int) Math.ceil(dCount / dPageSize);//页码数量

		 vo.setCurrent(pageNo);
		 vo.setHitCount(false);
		 vo.setOptimizeCountSql(true);
		 vo.setOrders(new ArrayList<>());
		 vo.setRecords(inquiryInfos);
		 vo.setPages(pages);
		 vo.setSearchCount(true);
		 vo.setSize(pageSize);
		 vo.setTotal(count);
		 return vo;
	 }
	
	/**
	 *   添加
	 *
	 * @param inquiryInfo
	 * @return
	 */
	@AutoLog(value = "inquiry_info-添加")
	@ApiOperation(value="inquiry_info-添加", notes="inquiry_info-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody InquiryInfo inquiryInfo) {
		inquiryInfoService.save(inquiryInfo);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param inquiryInfo
	 * @return
	 */
	@AutoLog(value = "inquiry_info-编辑")
	@ApiOperation(value="inquiry_info-编辑", notes="inquiry_info-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody InquiryInfo inquiryInfo) {
		inquiryInfoService.updateById(inquiryInfo);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "inquiry_info-通过id删除")
	@ApiOperation(value="inquiry_info-通过id删除", notes="inquiry_info-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		inquiryInfoService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "inquiry_info-批量删除")
	@ApiOperation(value="inquiry_info-批量删除", notes="inquiry_info-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.inquiryInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "inquiry_info-通过id查询")
	@ApiOperation(value="inquiry_info-通过id查询", notes="inquiry_info-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		InquiryInfo inquiryInfo = inquiryInfoService.getById(id);
		if(inquiryInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(inquiryInfo);
	}

	 /**
	  * 通过openid查询所有的问诊单列表
	  *
	  * @param patientId
	  * @return
	  */
	 @AutoLog(value = "inquiry_info-通过patient_id查询")
	 @ApiOperation(value="inquiry_info-通过patient_id查询", notes="inquiry_info-通过patient_id查询")
	 @GetMapping(value = "/queryInquiry")
	 public Result<?> queryInquiry(@RequestParam(name="patientId",required=true) String patientId) {
		//获取所有的问诊数据
	 	List<InquiryInfo> inquiryInfo = inquiryInfoService.getInquiryList(patientId);

	 	List response = new ArrayList();
	 	//关联问诊医生
		for(InquiryInfo one:inquiryInfo){
			Doctor doctor = doctorService.getById(one.getDoctorId());

			InquiryInfoVo vo = this.initInquiryInfoVo(doctor,one);
			response.add(vo);
		}
		return Result.ok(response);
	 }

	 /**
	  * 组合医生和问诊单的数据
	  * @param doctor
	  * @param inquiryInfo
	  * @return
	  */
	 private InquiryInfoVo initInquiryInfoVo(Doctor doctor , InquiryInfo inquiryInfo){
		 InquiryInfoVo vo = new InquiryInfoVo();
		 vo.setId(inquiryInfo.getId());
		 vo.setDoctorId(doctor.getId());
		 vo.setDoctorName(doctor.getName());
		 vo.setCreateTime(inquiryInfo.getCreateTime());
		 return vo;
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param inquiryInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, InquiryInfo inquiryInfo) {
        return super.exportXls(request, inquiryInfo, InquiryInfo.class, "inquiry_info");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, InquiryInfo.class);
    }

}
